package com.muyu.cloud.commodity.controller;

import com.muyu.cloud.commodity.domain.model.Product;
import com.muyu.cloud.commodity.domain.model.ProductSku;
import com.muyu.cloud.commodity.domain.model.Brand;
import com.muyu.cloud.commodity.domain.model.Category;
import com.muyu.cloud.commodity.domain.model.SpecGroup;
import com.muyu.cloud.commodity.service.ProductRemoteService;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.product.domain.StockCheckReq;
import com.muyu.common.product.domain.StockCheckResp;
import com.muyu.common.product.domain.ProductInfo;
import com.muyu.common.product.domain.ProductSkuInfo;
import com.muyu.common.product.domain.BrandInfo;
import com.muyu.common.product.domain.CategoryInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品服务远程调用控制器
 * 专门为其他服务提供远程调用接口
 *
 * @author JiaCheng
 * @version 1.0
 * @since 2025-09-21
 */
@RestController
@RequestMapping("/product/remote")
@Tag(name = "ProductRemoteController", description = "商品服务远程调用控制器")
public class ProductRemoteController extends BaseController {

    @Autowired
    private ProductRemoteService productRemoteService;

    /**
     * 根据商品ID获取商品信息
     */
    @Operation(summary = "获取商品信息", description = "根据商品ID获取商品详细信息")
    @GetMapping("/info/{productId}")
    public Result<ProductInfo> getProductInfo(@PathVariable("productId") Long productId) {
        Result<Product> result = productRemoteService.getProductInfo(productId);
        if (Result.isSuccess(result) && result.getData() != null) {
            ProductInfo productInfo = productRemoteService.convertToProductInfo(result.getData());
            return Result.success(productInfo);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 根据SKU ID获取SKU信息
     */
    @Operation(summary = "获取SKU信息", description = "根据SKU ID获取SKU详细信息")
    @GetMapping("/sku/{skuId}")
    public Result<ProductSkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        Result<ProductSku> result = productRemoteService.getSkuInfo(skuId);
        if (Result.isSuccess(result) && result.getData() != null) {
            ProductSkuInfo skuInfo = productRemoteService.convertToProductSkuInfo(result.getData());
            return Result.success(skuInfo);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 根据品牌ID获取品牌信息
     */
    @Operation(summary = "获取品牌信息", description = "根据品牌ID获取品牌详细信息")
    @GetMapping("/brand/{brandId}")
    public Result<BrandInfo> getBrandInfo(@PathVariable("brandId") Long brandId) {
        Result<Brand> result = productRemoteService.getBrandInfo(brandId);
        if (Result.isSuccess(result) && result.getData() != null) {
            BrandInfo brandInfo = productRemoteService.convertToBrandInfo(result.getData());
            return Result.success(brandInfo);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 根据分类ID获取分类信息
     */
    @Operation(summary = "获取分类信息", description = "根据分类ID获取分类详细信息")
    @GetMapping("/category/{categoryId}")
    public Result<CategoryInfo> getCategoryInfo(@PathVariable("categoryId") Long categoryId) {
        Result<Category> result = productRemoteService.getCategoryInfo(categoryId);
        if (Result.isSuccess(result) && result.getData() != null) {
            CategoryInfo categoryInfo = productRemoteService.convertToCategoryInfo(result.getData());
            return Result.success(categoryInfo);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 批量获取商品信息
     */
    @Operation(summary = "批量获取商品信息", description = "根据商品ID列表批量获取商品信息")
    @PostMapping("/batch")
    public Result<List<ProductInfo>> getBatchProductInfo(@RequestBody List<Long> productIds) {
        Result<List<Product>> result = productRemoteService.getBatchProductInfo(productIds);
        if (Result.isSuccess(result) && result.getData() != null) {
            List<ProductInfo> productInfos = result.getData().stream()
                    .map(product -> productRemoteService.convertToProductInfo(product))
                    .collect(java.util.stream.Collectors.toList());
            return Result.success(productInfos);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 批量获取SKU信息
     */
    @Operation(summary = "批量获取SKU信息", description = "根据SKU ID列表批量获取SKU信息")
    @PostMapping("/sku/batch")
    public Result<List<ProductSkuInfo>> getBatchSkuInfo(@RequestBody List<Long> skuIds) {
        Result<List<ProductSku>> result = productRemoteService.getBatchSkuInfo(skuIds);
        if (Result.isSuccess(result) && result.getData() != null) {
            List<ProductSkuInfo> skuInfos = result.getData().stream()
                    .map(sku -> productRemoteService.convertToProductSkuInfo(sku))
                    .collect(java.util.stream.Collectors.toList());
            return Result.success(skuInfos);
        }
        return Result.error(result.getMsg());
    }

    /**
     * 检查商品库存
     */
    @Operation(summary = "检查商品库存", description = "检查指定SKU的库存是否充足")
    @PostMapping("/stock/check")
    public Result<StockCheckResp> checkStock(@RequestBody StockCheckReq req) {
        return productRemoteService.checkStock(req);
    }

    /**
     * 获取商品规格信息
     */
    @Operation(summary = "获取商品规格信息", description = "根据商品ID获取商品规格组和规格值信息")
    @GetMapping("/specs/{productId}")
    public Result<List<SpecGroup>> getProductSpecs(@PathVariable("productId") Long productId) {
        return productRemoteService.getProductSpecs(productId);
    }

}
